package ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AddNoteBean;
import com.fy.fyzf.bean.HousingDetailBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.HousingFllowListBean;
import com.fy.fyzf.dialog.LoopViewPopwindow;
import com.fy.fyzf.presenter.HouseDetailPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.HouseDetailCallBack;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EditFllowActivity extends BaseActivity<HouseDetailPresenter> implements HouseDetailCallBack {
    @BindView(R.id.rl_type)
    RelativeLayout rlType;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_staff_name)
    TextView tvStaffName;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_text_count)
    TextView tvTextCount;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.tv_type)
    TextView tvType;
    //用户类型 弹窗显示  来电 去电

    private AddNoteBean addNoteBean = new AddNoteBean();
    private int housingId;

    @Override
    protected HouseDetailPresenter createPresenter() {
        return new HouseDetailPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_edit_fllow;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("写跟进");
        addNoteBean.setFollowType(1);
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvTextCount.setText(""+s.toString().length()+"/100");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void initData() {
        super.initData();
        housingId = getIntent().getIntExtra("housingId", 0);
        tvStaffName.setText(SPUtils.getInstance(this).getString("name", ""));
        tvCompany.setText(SPUtils.getInstance(this).getString("company", ""));

    }

    @Override
    public void getHousingDetailSuccess(HousingDetailBean housingDetailBean) {

    }

    @Override
    public void onError() {

    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void editFollowSuccess(BaseData baseData) {
        AppUtils.showToast("跟进成功");
        EventBus.getDefault().post("editSuccess");
        finish();
    }

    @Override
    public void getHousingFllowListSuccess(HousingFllowListBean housingFllowListBean) {

    }

    private List<String> titlts ;
    private List<Integer> idS ;
    @OnClick({R.id.rl_type, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_type:
                titlts = new ArrayList<>();
                idS = new ArrayList<>();
                titlts.add("来电");
                titlts.add("去电");
                idS.add(0);
                idS.add(1);
                showPop(titlts,idS);
                break;
            case R.id.tv_ok:
                if (TextUtils.isEmpty(etContent.getText().toString().trim())) {
                    AppUtils.showToast("请输入内容");
                    return;
                }
                if (TextUtils.isEmpty(tvType.getText().toString().trim())) {
                    AppUtils.showToast("请选择类型");
                    return;
                }
                addNoteBean.setFollowRemark(etContent.getText().toString().trim());
                addNoteBean.setHousingId(housingId);

                mPresenter.editFollow(addNoteBean);
                break;
        }
    }

    private void showPop(List<String> titles, List<Integer> idS){

        LoopViewPopwindow loopViewPopwindow = new LoopViewPopwindow(this);
        loopViewPopwindow.setIds(idS).setTitleData(titles).setTitle("").showPop(rlType);

        loopViewPopwindow.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvType.setText(titles.get(loopViewPopwindow.position));
                addNoteBean.setFollowType(idS.get(loopViewPopwindow.position));

                loopViewPopwindow.dismiss();
            }
        });
    }
}